package tmp.selenium;

import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;

import junit.framework.TestCase;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
// This is the driver'TemplateWorkSheet import. You'll use this for instantiating a
// browser and making it do what you need.
// Selenium-IDE add the Pattern module because it'TemplateWorkSheet sometimes used for
// regex validations. You can remove the module if it'TemplateWorkSheet not used in your
// script.
// This is the driver'TemplateWorkSheet import. You'll use this for instantiating a
// browser and making it do what you need.
// Selenium-IDE add the Pattern module because it'TemplateWorkSheet sometimes used for
// regex validations. You can remove the module if it'TemplateWorkSheet not used in your
// script.

@SuppressWarnings("deprecation")
public class SeleniumTest extends TestCase {
	// We create our Selenium test case
	private static WebDriver driver = null;
	private static boolean init = false;
	private static CountDownLatch cdl = new CountDownLatch(countTestMethod());
	private int i = 0;

	private static int countTestMethod() {
		int count = 0;
		Method[] methods = SeleniumTest.class.getDeclaredMethods();
		for (Method method : methods) {
			if (method.getName().startsWith("test")) {
				count++;
			}
		}
		return count;
	}

	public void setUp() {

		if (!init) {
			System.setProperty("webdriver.chrome.driver", "D:\\system\\path\\chromedriver.exe");
//			driver = new ChromeDriver(ChromeDriverService.);
			driver = new ChromeDriver(new ChromeDriverService.Builder().usingPort(9990).build());
			new Thread() {
				public void run() {

					try {
						cdl.await();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} finally {
						driver.close();
					}
				}
			}.start();
			init = true;
			// driver.
		}
	}

	public void tearDown() {
		cdl.countDown();
	}

	public void testNew() throws Exception {
		// WebDriver driver=new HtmlUnitDriver(true);
		// System.setProperty("webdriver.firefox.bin",
		// "G:\\Mozilla Firefox\\firefox.exe");
		// WebDriver driver=new FirefoxDriver();
		// System.setProperty("webdriver.firefox.bin",
		// "C:\\Program Files (x86)\\Google\\Chrome\\Application");

		// System.setProperty("webdriver.chrome.driver","D:\\system\\path\\chromedriver.exe");
		// WebDriver driver=new ChromeDriver();
		// System.setProperty("webdriver.ie.driver","D:\\system\\path\\IEDriverServer.exe");
		// WebDriver driver = new InternetExplorerDriver();
		driver.get("http://localhost:8081/WebReport/ReportServer?reportlet=debug%2Fwrite%2Frow-insert.cpt&op=write");
		// System.out.println(driver.getPageSource());

		// System.out.println(driver.findElement(By.id("B4-0-9")).getText());
		// FileWriter file=new FileWriter(new
		// File("G:\\FineReport\\WebReport\\html\\tmp.html"));
		// final String pageAsText = driver.getPageSource();
		// // System.out.println(pageAsText);
		// file.write(pageAsText);
		// file.close();
		driver.findElement(By.id("general-2-0")).findElement(By.cssSelector(".x-emb-add")).click();
		driver.findElement(By.id("general-4-0")).findElement(By.cssSelector(".x-emb-add")).click();

		System.out.println(driver.findElement(By.id("general-7-0")).getText());
		// Thread.sleep(30000);
		// driver.close();
	}

	public void testNew2() throws Exception {
		// WebDriver driver=new HtmlUnitDriver(true);
		// System.setProperty("webdriver.firefox.bin",
		// "G:\\Mozilla Firefox\\firefox.exe");
		// WebDriver driver=new FirefoxDriver();
		// System.setProperty("webdriver.firefox.bin",
		// "C:\\Program Files (x86)\\Google\\Chrome\\Application");

		// System.setProperty("webdriver.chrome.driver","D:\\system\\path\\chromedriver.exe");
		// WebDriver driver=new ChromeDriver();
		// System.setProperty("webdriver.ie.driver","D:\\system\\path\\IEDriverServer.exe");
		// WebDriver driver = new InternetExplorerDriver();
		driver.get("http://localhost:8081/WebReport/ReportServer?reportlet=debug%2Fwrite%2Frow-insert.cpt&op=write");
		driver.findElement(By.id("general-2-0")).findElement(By.cssSelector(".x-emb-add")).click();
		driver.findElement(By.id("general-3-0")).findElement(By.cssSelector(".x-emb-add")).click();

		System.out.println(driver.findElement(By.id("general-7-0")).getText());
		// Thread.sleep(30000);
		// driver.close();
	}
}